//
//  ZWTabBarView.swift
//  ZWWeibo
//
//  Created by flyiron on 16/4/16.
//  Copyright © 2016年 flyiron. All rights reserved.
//

import UIKit

class ZWTabBarView: UITabBar {
    
    var composeButtonClickClosure: (()->())?
    
    //重写init方法添加compose按钮
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //重写layoutSubview方法 重新布局tabbar按钮
    override func layoutSubviews() {
        super.layoutSubviews()
        composeButton.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
//        print("===========")
        let buttonWidth = self.frame.width/5
        var index = 0
        for value in subviews {
//            print(value)
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                value.frame.size.width = buttonWidth
                value.frame.origin.x = buttonWidth*CGFloat(index)
                index += 1
                if index == 2 {
                    index += 1
                }
            }
            
        }
    }
    
    //点击按钮执行的方法
    func clickComposeButton() {
        composeButtonClickClosure?()
    }
    
    
    //懒加载composeBTN
    private lazy var composeButton: UIButton = {
        let button: UIButton = UIButton()
        button.addTarget(self, action: #selector(ZWTabBarView.clickComposeButton), forControlEvents: .TouchUpInside)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button") , forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        return button
    }()

}
